import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;

/**
 * @Author Weton Li
 * @Date 2021/3/24 14:15
 */
public class 字符串相加 {

    public static String stringAdd(String num1, String num2) {

        StringBuilder sb = new StringBuilder();
        int end1 = num1.length() - 1, end2 = num2.length() - 1; // 双指针指向字符串末尾
        int add = 0;
        while (end1 != -1 || end2 != -1 || add != 0) {

            int num11 = (end1 >= 0) ? num1.charAt(end1) - '0' : 0; // 默认两数位数相同，若不同，公平补0
            int num22 = (end2 >= 0) ? num2.charAt(end2) - '0' : 0;

            int res = num11+num22+add; // 11+1= 12

            sb.append(res%10);
            add = res/10;


            end1--;
            end2--;

        }

        sb.reverse();
        return sb.toString();
    }

    public static void main(String[] args) {
        String s = stringAdd("123", "458");
        System.out.println(s);

    }
}
